import os
import re

def find_image_names_in_code(root_directory):
    image_names = set()
    pattern = r'\[UIImage\s+imageNamed\s*?:\s*?@"(.*?)"\]'
    
    for root, dirs, files in os.walk(root_directory):
        for file in files:
            file_path = os.path.join(root, file)
            if file_path.endswith(('.m', '.mm', '.swift', '.xib', '.storyboard')):
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    matches = re.findall(pattern, content)
                    image_names.update(matches)
    return image_names

def find_unused_images(root_directory, image_files):
    used_image_names = find_image_names_in_code(root_directory)
    unused_images = set()
    
    for image_file in image_files:
        image_name = os.path.basename(image_file)
        if image_name not in used_image_names:
            unused_images.add(image_name)
    
    return unused_images

def find_png_jpg_files(directory):
    png_jpg_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg')):
                png_jpg_files.append(os.path.join(root, file))
    return png_jpg_files

def main():
    directory = input("请输入要检查的项目根目录: ")  # 输入要检查的项目根目录
    if not os.path.isdir(directory):
        print("错误：指定路径不存在或不是一个目录！")
        return
    
    image_files = find_png_jpg_files(directory)
    unused_images = find_unused_images(directory, image_files)
    
    print("未被使用的图片名称：")
    for image_name in unused_images:
        print(image_name)

if __name__ == "__main__":
    main()
